package com.jk.myimkit.imkit.widget.provide;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.RongContext;
import com.jk.myimkit.imkit.RongIM;
import com.jk.myimkit.imkit.model.ProviderTag;
import com.jk.myimkit.imkit.model.UIMessage;
import com.jk.myimkit.imkit.plugin.location.AMapRealTimeActivity;
import com.jk.myimkit.imkit.plugin.location.LocationManager;
import com.jk.myimkit.imkit.utilities.OptionsPopupDialog;
import com.jk.myimkit.imkit.utilities.PromptPopupDialog;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.location.message.RealTimeLocationStartMessage;
import io.rong.imlib.model.Message;

@ProviderTag(
        messageContent = RealTimeLocationStartMessage.class
)
public class RealTimeLocationMessageProvider extends IContainerItemProvider.MessageProvider<RealTimeLocationStartMessage> {
    public RealTimeLocationMessageProvider() {
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_share_location_message, (ViewGroup)null);
        RealTimeLocationMessageProvider.ViewHolder holder = new RealTimeLocationMessageProvider.ViewHolder();
        holder.message = (TextView)view.findViewById(16908308);
        view.setTag(holder);
        return view;
    }

    public Spannable getContentSummary(RealTimeLocationStartMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, RealTimeLocationStartMessage data) {
        return data != null && data.getContent() != null ? new SpannableString(context.getResources().getString(R.string.rc_real_time_location_summary)) : null;
    }

    public void onItemClick(final View view, int position, RealTimeLocationStartMessage content, final UIMessage message) {
        if (message.getMessageDirection() == Message.MessageDirection.SEND) {
            this.joinMap(view, message);
        } else {
            PromptPopupDialog dialog = PromptPopupDialog.newInstance(view.getContext(), "", view.getContext().getResources().getString(R.string.rc_real_time_join_notification));
            dialog.setPromptButtonClickedListener(new PromptPopupDialog.OnPromptButtonClickedListener() {
                public void onPositiveButtonClicked() {
                    RealTimeLocationMessageProvider.this.joinMap(view, message);
                }
            });
            dialog.show();
        }

    }

    private void joinMap(View view, UIMessage message) {
        List<String> mLocationShareParticipants = RongIMClient.getInstance().getRealTimeLocationParticipants(message.getConversationType(), message.getTargetId());
        int result = LocationManager.getInstance().joinLocationSharing();
        if (result == 0) {
            Intent intent = new Intent(view.getContext(), AMapRealTimeActivity.class);
            if (mLocationShareParticipants != null) {
                intent.putStringArrayListExtra("participants", (ArrayList)mLocationShareParticipants);
            }

            view.getContext().startActivity(intent);
        } else if (result == 1) {
            Toast.makeText(view.getContext(), R.string.rc_network_exception, Toast.LENGTH_SHORT).show();
        } else if (result == 2) {
            Toast.makeText(view.getContext(), R.string.rc_location_sharing_exceed_max, Toast.LENGTH_SHORT).show();
        }

    }

    public void onItemLongClick(View view, int position, RealTimeLocationStartMessage content, final UIMessage message) {
        RealTimeLocationMessageProvider.ViewHolder holder = (RealTimeLocationMessageProvider.ViewHolder)view.getTag();
        holder.longClick = true;
        if (view instanceof TextView) {
            CharSequence text = ((TextView)view).getText();
            if (text != null && text instanceof Spannable) {
                Selection.removeSelection((Spannable)text);
            }
        }

        Resources res = view.getContext().getResources();
        String[] items = new String[]{res.getString(R.string.rc_dialog_item_message_delete)};
        OptionsPopupDialog.newInstance(view.getContext(), items).setOptionsPopupDialogListener(new OptionsPopupDialog.OnOptionsItemClickedListener() {
            public void onOptionsItemClicked(int which) {
                if (which == 0) {
                    RongIM.getInstance().deleteMessages(new int[]{message.getMessageId()}, (RongIMClient.ResultCallback)null);
                }

            }
        }).show();
    }

    public void bindView(View v, int position, RealTimeLocationStartMessage content, UIMessage data) {
        RealTimeLocationMessageProvider.ViewHolder holder = (RealTimeLocationMessageProvider.ViewHolder)v.getTag();
        Drawable drawable;
        if (data.getMessageDirection() == Message.MessageDirection.SEND) {
            drawable = holder.message.getResources().getDrawable(R.drawable.rc_icon_rt_message_right);
            drawable.setBounds(0, 0, 29, 41);
            holder.message.setBackgroundResource(R.drawable.rc_ic_bubble_right);
            holder.message.setCompoundDrawablesWithIntrinsicBounds((Drawable)null, (Drawable)null, drawable, (Drawable)null);
            holder.message.setText(RongContext.getInstance().getResources().getString(R.string.rc_real_time_location_sharing));
        } else {
            drawable = holder.message.getResources().getDrawable(R.drawable.rc_icon_rt_message_left);
            drawable.setBounds(0, 0, 29, 41);
            holder.message.setBackgroundResource(R.drawable.rc_ic_bubble_left);
            holder.message.setCompoundDrawablesWithIntrinsicBounds(drawable, (Drawable)null, (Drawable)null, (Drawable)null);
            holder.message.setText(RongContext.getInstance().getResources().getString(R.string.rc_real_time_location_sharing));
        }

    }

    private static class ViewHolder {
        TextView message;
        boolean longClick;

        private ViewHolder() {
        }
    }
}
